home *** CD-ROM | disk | FTP | other *** search
- Path: kiwi.gen.nz!alastair
- From: alastair@kiwi.gen.nz (Alastair Shar)
- Newsgroups: comp.lang.c
- Subject: Re: Hiding a password
- Date: 10 Mar 1996 16:10:46 GMT
- Organization: Kiwi Internet node, Auckland, New Zealand
- Message-ID: <4huuu6$81c@three.kiwi.gen.nz>
- References: <1996Feb29.224936.137160@forest> <4he620$qf2@hpbblb.bbn.hp.com> <TANMOY.96Mar4172334@qcd.lanl.gov>
- NNTP-Posting-Host: kiwi.gen.nz
- X-Newsreader: TIN [version 1.2 PL2]
-
- : #include <stdio.h>
- : char * GetString_NoEcho()
- : {
- : char *temp;
- : int i=1;
- : while(temp[i-1]!=13) {
- : temp[i-1]=getch();
- : i++;
- : }
- : temp[i]=0;
- : }
- : int main(void) {
- : char *pwd = GetString_NoEcho();
- : printf("%s\n",pwd);
- : return 0;
- : }
-
- : Can you help me? Any idea what I am doing wrong? Should I change the
- : i-1 to i*=+5 to get it to work?
- Your indexing is fine if a little to complex. As c arrays start at 0, easier
- to initialise i as 0, and use temp[i] rather than temp[i-1]. (just
- curiously, where did you come up with i*=+5 ??)
- Your problem with getch is that the compiler has no idea where to find the
- function. As getch is platform specific, you wont find it in stdio.h. Under
- dos compilers it's usually in conio.h. Dunno where it is in Unix, you'd have
- to look for it in that case.
- Still, your main problem is *temp. Okay, so it's a pointer. But what is it
- pointing to? All declaring 'char *temp' does is allocate a place for an
- address(pointer) to a char. It wont actually allocate any memory for an
- array unless you hard coded it: ie char *temp="asfdasd" or whatever. (not
- that useful in this case)
- You basically have two ways to give your pointer something to point to. Either
- allocate some memory, or declare an array. In the first case, something like
- 'char *temp=malloc(21);' would allocate 20 characters for the password (the
- 21st for the null terminater). Unfortunatly, as your code stands, this
- wouln't help cause you have no means of passing the pointer to this back to
- your main function. By delaring 'char *GetString_NoEcho()' you imply that
- the function returns a char pointer, but you still need 'return(temp);' as
- the last line of the function to achieve this. Oh yeah, you'd also have to
- #include a header file that specifies malloc...
- Alternatively, you could declare an array in the main program, and pass it's
- address to the function eg 'char pwd[21]; GetString_NoEcho(pwd);' and change
- the function declaration to be 'void GetString_NoEcho(char temp[])'. In this
- case you dont need to return an address cause you already have it in the
- main program, and you have to remove the 'char *temp' declaration in the
- function.
- In both these cases, you have to keep a check on how many characters are
- entered in the password. If more than 20 are entered, you end up writing
- into memory that hasn't been allocated for that purpose. Sometimes nothing
- odd happens... then again, just as likely, you'll end up having to reset
- your computer.
-
- hope this helps...
-
-
-
- --------------------------------------------------------------------------
- It began as a regular day in my room,
- with a cup of hot black coffee... <meryn cadell>
- --------------------------------------------------------------------------
-